package org.op.nav;

import java.awt.Component;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreeModel;
import javax.swing.tree.TreeNode;
import javax.swing.tree.TreePath;
import javax.swing.tree.TreeSelectionModel;

import org.op.service.WwDocumentInfo;
import org.op.service.WwFolderInfo;
import org.op.service.WwInfoObject;

public class NavigationViewImpl implements NavigationView, MouseListener {

	private Component component;
	private NavigationController navController;
	private TreeModel model; // model backing the component
	private JTree tree;
	
	private NavigatorPopupMenuView popupMenuView;
	
	public NavigationViewImpl() {
	}
	
	public void build() {
		
		model = navController.loadTreeModel(model);
		
		tree = new JTree(model);
		tree.setShowsRootHandles(true);
		tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
		tree.addMouseListener(this);
		
		JScrollPane pane = new JScrollPane(tree);
		//pane.setBorder(new Border(5,5));
		
        component = pane;
	}
	
	public void updateModel(TreeModel tm) {
		// get the expansion paths
		//Enumeration paths = tree.getExpandedDescendants(tree.getPathForRow(0));
		tree.setModel(tm);
		model = tm;
	}
	
	public TreeModel getModel() {
		return model;
	}

	public void mouseClicked(MouseEvent me) {
		if (me.getClickCount() == 2) {    
			TreeNode node = (TreeNode)tree.getLastSelectedPathComponent();
			navController.openNode(node);
		} else {
			// right click context menu?
			if (me.getButton() == MouseEvent.BUTTON3) {
				// if nothing is selected, select the closest row
				TreePath selPath = tree.getPathForLocation(me.getX(), me.getY());
				if (selPath!=null) {
					tree.getSelectionModel().setSelectionPath(selPath);
					WwInfoObject infoObject = getSelectedNodeObject();
					if (infoObject instanceof WwDocumentInfo) {
						popupMenuView.showMenu(tree, me.getPoint(), NavigatorPopupMenuView.NODE_TYPE_DOCUMENT);
					}
					if (infoObject instanceof WwFolderInfo) {
						popupMenuView.showMenu(tree, me.getPoint(), NavigatorPopupMenuView.NODE_TYPE_FOLDER);					
					}
				}
			}
		}
	}

	
	public WwInfoObject getSelectedNodeObject() {
		WwInfoObject infoObject = null;
		DefaultMutableTreeNode node = (DefaultMutableTreeNode)tree.getLastSelectedPathComponent();
		if (node!=null) {
			infoObject = (WwInfoObject)node.getUserObject();
		}
		return infoObject;
	}

	public void mouseEntered(MouseEvent arg0) {
	}

	public void mouseExited(MouseEvent arg0) {
	}

	public void mousePressed(MouseEvent arg0) {
	}

	public void mouseReleased(MouseEvent arg0) {
	}

	public Component getComponent() {
		return component;
	}

	public void setComponent(Component component) {
		this.component = component;
	}

	public NavigationController getNavController() {
		return navController;
	}

	public void setNavController(NavigationController navController) {
		this.navController = navController;
	}

	public NavigatorPopupMenuView getPopupMenuView() {
		return popupMenuView;
	}

	public void setPopupMenuView(NavigatorPopupMenuView popupMenuView) {
		this.popupMenuView = popupMenuView;
	}

}
